#########################################################################
# Figure2_b.R
# Will be used in Figure2.R
#########################################################################

#########################################################################
# 1. Process Data (US House)
#########################################################################

dt <- read.csv("qualtrics_Alaska_nonnumeric.csv")[-c(1:2), ] %>%
  mutate(id = row_number())

# Set a reference order

ref_set <- c("Begich", "Bye", "Palin", "Peltola")


#########################################################################
# 2. Average Ranks (US House)
#########################################################################

# Decompose ballot position x candidate

dtc <- dt %>%
  separate(C4_DO,
    into = c(
      "C4_pos1", "C4_pos2", "C4_pos3",
      "C4_pos4"
    ),
    sep = "[|]", remove = F
  ) %>% # Separate by "|"
  dplyr::select(id, starts_with("C4"), -c("C4_DO")) %>%
  pivot_longer(
    cols = C4_1:C4_4,
    values_to = "rank"
  ) %>%
  dplyr::select(id, name, rank, everything()) %>%
  mutate(
    name = case_when(
      name == "C4_1" ~ ref_set[1],
      name == "C4_2" ~ ref_set[2],
      name == "C4_3" ~ ref_set[3],
      name == "C4_4" ~ ref_set[4]
    ),
    rank = as.numeric(rank)
  )



g_list <- list()
treat_vec <- c("1st", "2nd", "3rd", "4th")


for (j in 1:4) { # For all candidates j = 1,...,10

  # Extracting only one candidate
  temp <- dtc %>%
    filter(name == ref_set[j])

  est <- NA
  upp <- NA
  low <- NA
  treat_name <- NA

  for (t in 1:4) { # For all positions t = 1,...,10s

    treated_pos <- paste0("C4_pos", t)
    treated_pos_v <- temp[treated_pos] %>% pull()

    temp_t <- temp %>%
      mutate(treated = ifelse(grepl(ref_set[1], treated_pos_v),
        1, # Candidate j is in position t (treated)
        0
      )) %>% # Otherwise (control)
      dplyr::select(id, name, rank, treated, everything())


    m <- lm(rank ~ treated, temp_t) # Computing ballot order effect
    est[t] <- summary(m)$coefficients[2, 1]
    upp[t] <- confint(m, level = 0.95)[2, 2] # 97.5%
    low[t] <- confint(m, level = 0.95)[2, 1] # 2.5%
    treat_name[t] <- treat_vec[t]
  }


  g_list[[j]] <- t(rbind(est, upp, low)) %>%
    as_tibble() %>%
    mutate(
      cand = ref_set[j],
      ps = "Average Rank Effects",
      treatment = treat_name
    ) %>%
    data.frame()
}


g_list

g_out <- as.data.frame(do.call(rbind, g_list))
g_out$treatment <- factor(g_out$treatment, levels = treat_vec)
g_out$col <- ifelse(g_out$low > 0, "A", "C")
g_out$col <- ifelse(g_out$upp < 0, "B", g_out$col)
g_out$cand <- factor(g_out$cand, levels = rev(sort(unique(g_out$cand))))

p <- ggplot(g_out, aes(x = cand, y = est)) +
  geom_hline(yintercept = 0, colour = "black", lty = 2, lwd = 0.5) +
  geom_point(aes(x = cand, y = est, color = col)) + # Reorder by point estimate
  geom_linerange(aes(x = cand, ymin = low, ymax = upp, color = col), lwd = 0.5) +
  facet_grid(cols = vars(treatment)) +
  scale_colour_manual(values = c("#b0015a", "#128ba0", "gray")) +
  theme_classic() +
  expand_limits(y = c(-1, 1)) +
  scale_y_reverse(limits = c(1, -1)) +
  ylab("") +
  xlab("") +
  ggtitle("US House, Alaska (Average Ranks)") +
  theme(
    legend.position = "none", plot.margin = margin(0.2, 0.2, 0.2, -0.2, "cm"),
    strip.text.x = element_text(color = "dimgray", size = 7), text = element_text(size = 8)
  ) +
  scale_y_continuous(guide = guide_axis(angle = 90)) +
  coord_flip()

p


#########################################################################
# 3. Candidate Selection (US House)
#########################################################################

# Decompose ballot position x candidate

dtc <- dt %>%
  separate(C3_DO,
    into = c(
      "C3_pos1", "C3_pos2", "C3_pos3",
      "C3_pos4"
    ),
    sep = "[|]", remove = F
  ) %>% # Separate by "|"
  dplyr::select(id, starts_with("C3"), -c("C3_DO")) %>%
  pivot_longer(
    cols = C3_1:C3_4,
    values_to = "rank"
  ) %>%
  mutate(
    name = case_when(
      name == "C3_1" ~ ref_set[1],
      name == "C3_2" ~ ref_set[2],
      name == "C3_3" ~ ref_set[3],
      name == "C3_4" ~ ref_set[4]
    ),
    rank = as.numeric(rank),
    selection = ifelse(is.na(rank), 0, 1)
  ) %>%
  dplyr::select(id, name, selection, rank, everything())


g_list <- list()

for (j in 1:4) { # For all candidates j = 1,...,10

  # Extracting only one candidate
  temp <- dtc %>%
    filter(name == ref_set[j])

  est <- NA
  upp <- NA
  low <- NA
  treat_name <- NA

  for (t in 1:4) { # For all positions t = 1,...,10s

    treated_pos <- paste0("C3_pos", t)
    treated_pos_v <- temp[treated_pos] %>% pull()

    temp_t <- temp %>%
      mutate(treated = ifelse(grepl(ref_set[1], treated_pos_v),
        1, # Candidate j is in position t (treated)
        0
      )) %>% # Otherwise (control)
      dplyr::select(id, name, rank, treated, everything())


    m <- lm(selection ~ treated, temp_t) # Computing ballot order effect under Assumptions 9
    est[t] <- summary(m)$coefficients[2, 1]
    upp[t] <- confint(m, level = 0.95)[2, 2] # 97.5%
    low[t] <- confint(m, level = 0.95)[2, 1] # 2.5%
    treat_name[t] <- treat_vec[t]
  }


  g_list[[j]] <- t(rbind(est, upp, low)) %>%
    as_tibble() %>%
    mutate(
      cand = ref_set[j],
      ps = "Average Rank Effects",
      treatment = treat_name
    ) %>%
    data.frame()
}


g_list

g_out <- as.data.frame(do.call(rbind, g_list))
g_out$treatment <- factor(g_out$treatment, levels = treat_vec)
g_out$col <- ifelse(g_out$low > 0, "A", "C")
g_out$col <- ifelse(g_out$upp < 0, "B", g_out$col)
g_out$cand <- factor(g_out$cand, levels = rev(sort(unique(g_out$cand))))


p3 <- ggplot(g_out, aes(x = cand, y = est)) +
  geom_hline(yintercept = 0, colour = "black", lty = 2, lwd = 0.5) +
  geom_point(aes(x = cand, y = est, color = col)) + # Reorder by point estimate
  geom_linerange(aes(x = cand, ymin = low, ymax = upp, color = col), lwd = 0.5) +
  facet_grid(cols = vars(treatment)) +
  scale_colour_manual(values = c("#128ba0", "gray")) +
  theme_classic() +
  expand_limits(y = c(-0.5, 0.5)) +
  ylim(-0.5, 0.5) +
  ylab("") +
  xlab("") +
  ggtitle("US House, Alaska (Candidate Selection)") +
  theme(
    legend.position = "none", plot.margin = margin(0.2, 0.2, 0.2, -0.2, "cm"),
    strip.text.x = element_text(color = "dimgray", size = 7), text = element_text(size = 8)
  ) +
  scale_y_continuous(guide = guide_axis(angle = 90)) +
  coord_flip()
p3


#########################################################################
# 4. Data Processing (US Senate)
#########################################################################

rm(dt, dtc)

dt <- read.csv("qualtrics_Alaska_nonnumeric.csv")[-c(1:2), ] %>%
  mutate(id = row_number())


# Set a reference order

ref_set <- c("Chesbro", "Kelley", "Murkowski", "Thibaka")


#########################################################################
# 5. Average Ranks (US Senate)
#########################################################################

# Decompose ballot position x candidate

dtc <- dt %>%
  separate(C6_DO,
    into = c(
      "C6_pos1", "C6_pos2", "C6_pos3",
      "C6_pos4"
    ),
    sep = "[|]", remove = F
  ) %>% # Separate by "|"
  dplyr::select(id, starts_with("C6"), -c("C6_DO")) %>%
  pivot_longer(
    cols = C6_1:C6_4,
    values_to = "rank"
  ) %>%
  dplyr::select(id, name, rank, everything()) %>%
  mutate(
    name = case_when(
      name == "C6_1" ~ ref_set[1],
      name == "C6_2" ~ ref_set[2],
      name == "C6_3" ~ ref_set[3],
      name == "C6_4" ~ ref_set[4]
    ),
    rank = as.numeric(rank)
  )



g_list <- list()
treat_vec <- c("1st", "2nd", "3rd", "4th")


for (j in 1:4) { # For all candidates j = 1,...,10

  # Extracting only one candidate
  temp <- dtc %>%
    filter(name == ref_set[j])

  est <- NA
  upp <- NA
  low <- NA
  treat_name <- NA

  for (t in 1:4) { # For all positions t = 1,...,10s

    treated_pos <- paste0("C6_pos", t)
    treated_pos_v <- temp[treated_pos] %>% pull()

    temp_t <- temp %>%
      mutate(treated = ifelse(grepl(ref_set[1], treated_pos_v),
        1, # Candidate j is in position t (treated)
        0
      )) %>% # Otherwise (control)
      dplyr::select(id, name, rank, treated, everything())


    m <- lm(rank ~ treated, temp_t) # Computing ballot order effect under Assumptions 9
    est[t] <- summary(m)$coefficients[2, 1]
    upp[t] <- confint(m, level = 0.95)[2, 2] # 97.5%
    low[t] <- confint(m, level = 0.95)[2, 1] # 2.5%
    treat_name[t] <- treat_vec[t]
  }


  g_list[[j]] <- t(rbind(est, upp, low)) %>%
    as_tibble() %>%
    mutate(
      cand = ref_set[j],
      ps = "Average Rank Effects",
      treatment = treat_name
    ) %>%
    data.frame()
}


g_list

g_out <- as.data.frame(do.call(rbind, g_list))
g_out$treatment <- factor(g_out$treatment, levels = treat_vec)
g_out$col <- ifelse(g_out$low > 0, "A", "C")
g_out$col <- ifelse(g_out$upp < 0, "B", g_out$col)
g_out$cand <- factor(g_out$cand, levels = rev(sort(unique(g_out$cand))))


p2 <- ggplot(g_out, aes(x = cand, y = est)) +
  geom_hline(yintercept = 0, colour = "black", lty = 2, lwd = 0.5) +
  geom_point(aes(x = cand, y = est, color = col)) + # Reorder by point estimate
  geom_linerange(aes(x = cand, ymin = low, ymax = upp, color = col), lwd = 0.5) +
  facet_grid(cols = vars(treatment)) +
  scale_colour_manual(values = c("#b0015a", "#128ba0", "gray")) +
  theme_classic() +
  expand_limits(y = c(-1, 1)) +
  scale_y_reverse(limits = c(1, -1)) +
  ylab("") +
  xlab("") +
  ggtitle("US Senate, Alaska (Average Ranks)") +
  theme(
    legend.position = "none",
    plot.margin = margin(0.2, 0.2, 0.2, -0.2, "cm"),
    strip.text.x = element_text(color = "dimgray", size = 7), text = element_text(size = 8)
  ) +
  scale_y_continuous(guide = guide_axis(angle = 90)) +
  coord_flip()
p2


#########################################################################
# 6. Candidate Selection (US Senate)
#########################################################################

# Decompose ballot position x candidate

dtc <- dt %>%
  separate(C5_DO,
    into = c(
      "C5_pos1", "C5_pos2", "C5_pos3",
      "C5_pos4"
    ),
    sep = "[|]", remove = F
  ) %>% # Separate by "|"
  dplyr::select(id, starts_with("C5"), -c("C5_DO")) %>%
  pivot_longer(
    cols = C5_1:C5_4,
    values_to = "rank"
  ) %>%
  mutate(
    name = case_when(
      name == "C5_1" ~ ref_set[1],
      name == "C5_2" ~ ref_set[2],
      name == "C5_3" ~ ref_set[3],
      name == "C5_4" ~ ref_set[4]
    ),
    rank = as.numeric(rank),
    selection = ifelse(is.na(rank), 0, 1)
  ) %>%
  dplyr::select(id, name, selection, rank, everything())



g_list <- list()

for (j in 1:4) { # For all candidates j = 1,...,10

  # Extracting only one candidate
  temp <- dtc %>%
    filter(name == ref_set[j])

  est <- NA
  upp <- NA
  low <- NA
  treat_name <- NA

  for (t in 1:4) { # For all positions t = 1,...,10s

    treated_pos <- paste0("C5_pos", t)
    treated_pos_v <- temp[treated_pos] %>% pull()

    temp_t <- temp %>%
      mutate(treated = ifelse(grepl(ref_set[1], treated_pos_v),
        1, # Candidate j is in position t (treated)
        0
      )) %>% # Otherwise (control)
      dplyr::select(id, name, rank, treated, everything())


    m <- lm(selection ~ treated, temp_t) # Computing ballot order effect under Assumptions 9
    est[t] <- summary(m)$coefficients[2, 1]
    upp[t] <- confint(m, level = 0.95)[2, 2] # 97.5%
    low[t] <- confint(m, level = 0.95)[2, 1] # 2.5%
    treat_name[t] <- treat_vec[t]
  }


  g_list[[j]] <- t(rbind(est, upp, low)) %>%
    as_tibble() %>%
    mutate(
      cand = ref_set[j],
      ps = "Average Selection Effects",
      treatment = treat_name
    ) %>%
    data.frame()
}


g_list

g_out <- as.data.frame(do.call(rbind, g_list))
g_out$treatment <- factor(g_out$treatment, levels = treat_vec)
g_out$col <- ifelse(g_out$low > 0, "A", "C")
g_out$col <- ifelse(g_out$upp < 0, "B", g_out$col)
g_out$cand <- factor(g_out$cand, levels = rev(sort(unique(g_out$cand))))


p4 <- ggplot(g_out, aes(x = cand, y = est)) +
  geom_hline(yintercept = 0, colour = "black", lty = 2, lwd = 0.5) +
  geom_point(aes(x = cand, y = est, color = col)) +
  geom_linerange(aes(x = cand, ymin = low, ymax = upp, color = col), lwd = 0.5) +
  facet_grid(cols = vars(treatment)) +
  scale_colour_manual(values = c("#b0015a", "gray")) +
  theme_classic() +
  expand_limits(y = c(-0.5, 0.5)) +
  ylim(-0.5, 0.5) +
  ylab("") +
  xlab("") +
  ggtitle("US Senate, Alaska (Candidate Selection)") +
  theme(
    legend.position = "none", plot.margin = margin(0.2, 0.2, 0.2, -0.2, "cm"),
    strip.text.x = element_text(color = "dimgray", size = 7), text = element_text(size = 8)
  ) +
  scale_y_continuous(guide = guide_axis(angle = 90)) +
  coord_flip()
p4


#########################################################################
# 6. Combining All Panels
#########################################################################

p_alaska <- ggarrange(p, p2, p3, p4,
  ncol = 2, nrow = 2
)
